package com.example.myapplication.adapter;

import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import com.example.myapplication.data.Record;
import com.example.myapplication.databinding.ItemRecordBinding;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.ViewHolder> {
    private List<Record> records;
    private SimpleDateFormat dateFormat;
    private OnRecordActionListener listener;

    public interface OnRecordActionListener {
        void onRecordEdit(Record record);
        void onRecordDelete(Record record);
    }

    public RecordAdapter(List<Record> records, OnRecordActionListener listener) {
        this.records = records;
        this.listener = listener;
        this.dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRecordBinding binding = ItemRecordBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Record record = records.get(position);
        
        // 设置类别图标
        int iconResId = getCategoryIconResId(record.getCategory());
        holder.binding.categoryIcon.setImageResource(iconResId);
        
        // 设置类别名称
        holder.binding.categoryName.setText(record.getCategory());
        
        // 设置备注
        holder.binding.recordNote.setText(record.getNote());
        
        // 设置金额
        String amountText = (record.isExpense() ? "-" : "+") + String.format("%.2f", record.getAmount());
        holder.binding.recordAmount.setText(amountText);
        holder.binding.recordAmount.setTextColor(holder.itemView.getContext().getColor(R.color.white));
        holder.binding.recordAmount.setAlpha(record.isExpense() ? 1.0f : 0.8f);
        
        // 设置日期
        holder.binding.recordDate.setText(dateFormat.format(new Date(record.getDate())));

        // 设置长按事件
        holder.itemView.setOnLongClickListener(v -> {
            showActionDialog(v.getContext(), record);
            return true;
        });
    }

    private void showActionDialog(android.content.Context context, Record record) {
        String[] options = {"编辑", "删除"};
        new AlertDialog.Builder(context)
                .setItems(options, (dialog, which) -> {
                    switch (which) {
                        case 0: // 编辑
                            if (listener != null) {
                                listener.onRecordEdit(record);
                            }
                            break;
                        case 1: // 删除
                            showDeleteConfirmDialog(context, record);
                            break;
                    }
                })
                .show();
    }

    private void showDeleteConfirmDialog(android.content.Context context, Record record) {
        new AlertDialog.Builder(context)
                .setTitle("删除记录")
                .setMessage("确定要删除这条记录吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    if (listener != null) {
                        listener.onRecordDelete(record);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    public int getItemCount() {
        return records.size();
    }

    public void updateRecords(List<Record> newRecords) {
        this.records = newRecords;
        notifyDataSetChanged();
    }

    private int getCategoryIconResId(String category) {
        // 根据类别名称返回对应的图标资源ID
        switch (category) {
            // 支出类别
            case "餐饮": return R.drawable.eat;
            case "购物": return R.drawable.shopping;
            case "零食": return R.drawable.retail;
            case "水果": return R.drawable.fruit;
            case "交通": return R.drawable.transport;
            case "修车": return R.drawable.car;
            case "学习": return R.drawable.education;
            case "礼物": return R.drawable.gift;
            case "宠物": return R.drawable.pet;
            case "游戏": return R.drawable.game;
            case "话费": return R.drawable.phone;
            case "水电": return R.drawable.lifefee;
            case "运动": return R.drawable.sport;
            case "仪容": return R.drawable.skin;
            
            // 收入类别
            case "生活费": return R.drawable.salary;
            case "副业": return R.drawable.vicejob;
            case "奖金": return R.drawable.bonus;
            case "理财": return R.drawable.financing;
            case "其他": return R.drawable.other;  // 通用的其他类别
            default: return R.drawable.other;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ItemRecordBinding binding;

        ViewHolder(ItemRecordBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
} 